package cn.kkcxy.blog.system.factory.login;

import cn.kkcxy.blog.core.enums.LoginTypeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: kkcxy-blog
 * @BelongsPackage: cn.kkcxy.blog.system.factory
 * @Author: kkcxy
 * @CreateTime: 2024-09-11 22:43
 * @Description: 登录类型工厂
 * @Version: 1.0
 */
@Slf4j
@Component
public class LoginTypeHandlerFactory implements InitializingBean {

    /**
     * 登录处理的集合
     */
    @Resource
    private List<LoginTypeHandler> loginTypeHandlerList;

    /**
     * 使用 K - V 存储处理器集合 (K = 登录类型枚举, V = 对应登录处理器)
     */
    private final Map<LoginTypeEnum, LoginTypeHandler> handlerMap = new HashMap<>();

    /**
     * 得到登录类型处理器
     */
    public LoginTypeHandler getHandlerByLoginType(int loginType) {
        LoginTypeEnum typeEnum = LoginTypeEnum.getType(loginType);
        return handlerMap.get(typeEnum);
    }

    /**
     * 初始化 map
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        for (LoginTypeHandler typeHandler : loginTypeHandlerList) {
            handlerMap.put(typeHandler.getHandlerType(), typeHandler);
        }
    }

}
